<?php  

namespace app\Index\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use think\File;


class User extends Index

{    
	 public function sign()
    {   




        //接收post数据
        $list = Request::instance()->post();
        



        // var_dump($list);exit;


          if(empty($list['phone'])&&empty($list['password'])&&empty($list['repassword'])){
                                return $this->success('请填写值');
                            }

        // 1. 正则验证
                        // 判断手机号长度
                        if(!preg_match('/^[0-9]{11,11}/',$list['phone'])){
                             return $this->success('手机长度不够');
                        }   
                        // 判断手机号开头号码
                        if(!preg_match('/^[1][34578]/',$list['phone'])){
                            return $this->success('手机格式不对');
                        }

                        // 判断密码格式是否正确
                        if(!preg_match('/^[0-9]{5,17}$/',$list['password'])){
                               return $this->success('密码格式不对');
                            }

                        // 判断两次密码是否一致
                            if( $list['password'] != $list['repassword']){
                              return $this->success('两次密码不一致');
                            }


        //处理数据
        $time=date('Y-m-d H:i:s',time());
        $data=['phone'=>$list['phone'],'password'=>$list['password'],'createtime'=>$time];
        //执行添加操作
        
       $result= Db::name('tb_users')->insertGetid($data);

       if ($result >0) {
          $jifen=Db::name('to_jifen')->data(['uid'=>$result,'jifen'=>0])->insert();
          return $this->success('注册成功',url('index/index/index'));
       }else{
          return $this->success('注册失败');
       }

    }


    public function login()
    {   
    	//接收post数据
    	 $list = Request::instance()->post();
    	  // var_dump($list);exit;
    	 //搜索数据库的数据
    	
    	 $data= Db::name('tb_users')->where('phone',$list['phone'])->value('password');
    	
         
    	if ($list['password']==$data) {
    		Session::set('user.phone',$list['phone']);
    	
    		return $this->success('登陆成功',url('index/user/logingo'));
    	
    	 
    	
    	}else{

    		return $this->success('登录失败');
    	}
      
    }

    public function logingo()
    {
    

    	 $this->assign('phone',Session::get('user.phone') );







       return $this->index();

    }


    public function out()
    {   

        
        Session::set('user.phone',null);

        return $this->index();

    }


        public function pcenter()
    {   $this->assign('phone',Session::get('user.phone') );

	    $row = Db::name('tb_users')->where('phone',Session::get('user.phone'))->select();
	    $data=$row[0];
	    // var_dump($data);  
	    $this->assign('data',$data);    	 

      $jifen = Db::name('to_jifen')->where('uid',$data['id'])->select();
      // var_dump($jifen);die;
      $ji = $jifen[0];
      $this->assign('ji',$ji);
        return $this->fetch('pcenter/zhuye');
    }

        public function set()
    {     

    	    $this->assign('phone',Session::get('user.phone') );

    	    $row = Db::name('tb_users')->where('phone',Session::get('user.phone'))->select();
		      $data=$row[0];
		      // var_dump($data);  
		      $this->assign('data',$data);    	

            return $this->fetch('set-up/set-up');
 
    }

       public function update(Request $request)
    {
 
    // 获取表单上传文件
        $file = $request->file('image');
        if (empty($file)) {
            $this->error('请选择上传文件');
        }
    // 移动到框架应用根目录/public/uploads/ 目录下
       $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
       $icon=$info->getSaveName();
       $icon = str_replace('\\','/',$icon);
       // var_dump($icon);die;
       

      
      

       
   //     if ($info) {
   //         $this->success('文件上传成功：' . $info->getRealPath());
   //     } else {
   // // 上传失败获取错误信息
   //         $this->error($file->getError());
   //     }
 




    	 $this->assign('phone',Session::get('user.phone') );
         //获取post数据
         $p = Request::instance()->post();
         // var_dump($p);exit;
         $birthday=$p['year'].'-'.$p['month'].'-'.$p['day'];
         $address=$p['s_province'].'-'.$p['s_city'];
         //处理数据
         $data = ['name'=>$p['name'],
                  'introduce'=>$p['introduce'],
                  'sex'=>$p['sex'],
                  'birthday'=>$birthday,
                  'address'=>$address,
                  'icon'=>$icon
                  
            ];
         

          //指定更新
          $result = Db::name('tb_users')->where('phone',Session::get('user.phone'))->update($data);
          //判断执行情况
          if ($result > 0){
          	return $this->success('更新成功',url('index/user/pcenter'));
          
    }else{

    	   	return $this->success('更新失败');
         }

     }

    


}
   